var testTimeArray;

var testInfoArray;

var answerArray = new Array();

var answerContentArray = new Array();

var testIndex = 0;

var testLoaded = true;

var testHided = true;

var answerTypeArray = new Array();

var testStateArray = new Array();

var testExitIndex = "";

var testInTheEnd = false;

var testInTheEndArray = new Array();

var lastTestShow = false;

var testAnswers = "( )";

var textResultIndex = 0;

var textConfirmText = [
  { submitTest: "请作答后提交!", submitCancel: "取消", subMitConfirm: "确定" },
  {
    submitTest: "回答正确!",
    submitCancel: "查看试题",
    subMitConfirm: "返回视频播放",
  },
  {
    submitTest: "正确答案为:",
    submitCancel: "查看试题",
    subMitConfirm: "返回视频播放",
  },
];

var submitAnswer = "";

//解析考题文件
function getTest() {
  testTimeArray = new Array();

  testInfoArray = new Array();

  var index = 0;

  for (var i = 0; i < parent.test.length; i++) {
    if (parent.test[i][0] != "-1" && parent.test[i][0] != "") {
      testTimeArray.push(parent.test[i][0]);

      testInfoArray[index] = new Array();

      for (var j = 1; j < parent.test[i].length; j++) {
        var tmpArray = new Array();

        tmpArray.push(parent.test[i][j]);

        var tmpVarName = "answer" + i + j;

        var answer = parent.eval(tmpVarName);

        for (k = 0; k < answer.length; k++) tmpArray.push(answer[k]);

        testInfoArray[index].push(tmpArray);
      }

      var state = "";

      testStateArray.push(state);

      index++;
    }
  }
}

//显示考题

function showTest(index) {
  var testContent = "";

  testIndex = index;

  testStateArray[testIndex] = "dirty";

  for (var i = 0; i < testInfoArray[index].length; i++) {
    answerArray[i] = new Array();

    answerContentArray[i] = "";

    answerTypeArray[i] = new Array();

    var keyWords = testInfoArray[index][i][1].split(";");

    var keyWord = "";

    var testType = 0;

    if (keyWords.length > 1) {
      keyWord = "(多选题)";

      testType = 1;

      answerTypeArray[i][0] = "multiple";
    } else {
      keyWord = "(单选题)";

      answerTypeArray[i][0] = "single";
    }
    answerTypeArray[i][1] = "undirtied";

    testContent =
      testContent +
      "<div class='test-title' id='testTitle'" +
      i +
      ">" +
      keyWord +
      ":&nbsp;" +
      testInfoArray[index][i][0] +
      "&nbsp;</div><ul>";

    for (var j = 2; j < testInfoArray[index][i].length; j++) {
      if (testType == 0)
        testContent =
          testContent +
          "<li><input type='radio' name='single_answers' class='checkBoxData' onclick='onClickSingle(" +
          i +
          "," +
          j +
          ")'/>" +
          "<span>" +
          testInfoArray[index][i][j] +
          "</span></li>";
      else if (testType == 1)
        testContent =
          testContent +
          "<li><input type='checkbox' name='multi_answers' class='checkBoxData' onchange='onClickMultiple(this," +
          i +
          "," +
          j +
          ")'/>" +
          "<span>" +
          testInfoArray[index][i][j] +
          "</span></li>";
    }

    testContent = testContent + "</ul>";
  }

  document.getElementById("text-contents").innerHTML = testContent;
}

//清空选项

function onClearAnswer() {
  var index = testIndex;

  $(".checkBoxData").attr("checked", false);

  answerArray[index] = new Array();

  answerContentArray[index] = "";
}

//单选题

function onClickSingle(tIndex, tAnswer) {
  var code = getCodeFromNum(tAnswer);

  answerArray[tIndex][0] = tAnswer;
}

//多选题

function onClickMultiple(obj, tIndex, tAnswer) {
  if (obj.checked) {
    onInsertAnswer(tIndex, tAnswer);
  } else {
    onPopAnswer(tIndex, tAnswer);
  }
}

//删减选项

function onPopAnswer(tIndex, tAnswer) {
  var tAnswerCode = getCodeFromNum(tAnswer);

  var string = answerContentArray[tIndex];

  var i = string.indexOf(tAnswerCode);

  if (i == -1) {
    return;
  } else {
    var replaceString = " " + tAnswerCode;

    string = string.replace(replaceString, "");

    var num = getNumFromCode(tAnswerCode);

    removeByValue(answerArray[tIndex], num);
  }

  answerContentArray[tIndex] = string;
}

//新增选项

function onInsertAnswer(tIndex, tAnswer) {
  var tAnswerCode = getCodeFromNum(tAnswer);

  if (answerContentArray[tIndex].indexOf(tAnswerCode) == -1) {
    if (answerArray[tIndex].length == 0) answerArray[tIndex].push(tAnswer);
    else {
      var length = answerArray[tIndex].length;

      answerArray[tIndex][length] = tAnswer;

      for (var i = length - 1; i >= 0; i--) {
        if (answerArray[tIndex][i] > tAnswer) {
          answerArray[tIndex][i + 1] = answerArray[tIndex][i];

          answerArray[tIndex][i] = tAnswer;
        } else continue;
      }
    }

    var tmpAnswer = "";

    for (var j = 0; j < answerArray[tIndex].length; j++) {
      var code = getCodeFromNum(answerArray[tIndex][j]);

      tmpAnswer = tmpAnswer + " " + code;
    }

    answerContentArray[tIndex] = tmpAnswer;

    return answerContentArray[tIndex];
  } else {
    return false;
  }
}

function onSubmitAnswer(i, j) {
  var returnAnswer = onFilterAnswer(i, j);

  if (returnAnswer != false) {
    var tmpTitleId = "testAnswer" + i;

    document.getElementById(tmpTitleId).innerHTML = "(" + returnAnswer + ")";
  }
}

function onFilterAnswer(tIndex, tAnswer) {
  var tAnswerCode = getCodeFromNum(tAnswer);

  if (answerTypeArray[tIndex][0] == "single") {
    answerArray[tIndex][0] = tAnswer;

    var code = getCodeFromNum(answerArray[tIndex][0]);

    return code;
  } else {
    if (answerContentArray[tIndex].indexOf(tAnswerCode) == -1) {
      if (answerArray[tIndex].length == 0) answerArray[tIndex].push(tAnswer);
      else {
        var length = answerArray[tIndex].length;

        answerArray[tIndex][length] = tAnswer;

        for (var i = length - 1; i >= 0; i--) {
          if (answerArray[tIndex][i] > tAnswer) {
            answerArray[tIndex][i + 1] = answerArray[tIndex][i];

            answerArray[tIndex][i] = tAnswer;
          } else continue;
        }
      }
      var tmpAnswer = "";

      for (var j = 0; j < answerArray[tIndex].length; j++) {
        var code = getCodeFromNum(answerArray[tIndex][j]);
        tmpAnswer = tmpAnswer + " " + code;
      }
      answerContentArray[tIndex] = tmpAnswer;

      return answerContentArray[tIndex];
    } else return false;
  }
}

//从数字获取选项

function getCodeFromNum(i) {
  var s = String.fromCharCode(i + 63);

  return s;
}

//从选项获取数字

function getNumFromCode(i) {
  var s = parseInt(i.charCodeAt() - 63);

  return s;
}

//公共函数，数组中删除指定项

function removeByValue(arr, val) {
  for (var i = 0; i < arr.length; i++) {
    if (arr[i] == val) {
      arr.splice(i, 1);

      break;
    }
  }
}

function onEditAnswer(index) {
  var tmpTitleId = "testAnswer" + index;

  document.getElementById(tmpTitleId).innerHTML =
    "(&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)";

  answerArray[index] = new Array();

  answerContentArray[index] = "";

  $("#answerPicture" + index).css("background-image", "url(assets/blank.gif)");
}

//检查答题状态

function onCheckAnswer() {
  var index = testIndex;

  if (answerArray[index].length == 0) {
    textResultIndex = 0;

    alert(textConfirmText[textResultIndex].submitTest);

    return;
  } else {
    var tmpAnswer = "";

    for (var i = 0; i < answerArray[index].length; i++) {
      tmpAnswer = tmpAnswer + (answerArray[index][i] - 1) + ";";
    }

    var aimedAnswer = testInfoArray[testIndex][index][1] + ";";

    if (tmpAnswer == aimedAnswer) {
      answerTypeArray[index][1] = "answered";

      textResultIndex = 1;
    } else {
      answerTypeArray[index][1] = "answered";

      textResultIndex = 2;
    }
  }

  onShowAnswer(textResultIndex, index);
}

function onShowAnswer(resultIndex, index) {
  var keyWords = testInfoArray[testIndex][index][1].split(";");

  var keyWord = "";

  if (answerTypeArray[index][1] == "undirtied") {
    keyWord = "";
  } else {
    keyWord = "";

    for (var i = 0; i < keyWords.length; i++) {
      var num = parseInt(keyWords[i]) + 1;

      if (resultIndex == 2) keyWord = keyWord + getCodeFromNum(num) + ".";
    }
    answerTypeArray[index][1] = "answered";
  }
  $(".test-result").html(textConfirmText[resultIndex].submitTest + keyWord);

  /*   $(".confirm-text").html(textConfirmText[resultIndex].submitTest+keyWord);
		  
		  $(".cancel-button").html(textConfirmText[resultIndex].submitCancel);
		  
		  $(".confirm-button").html(textConfirmText[resultIndex].subMitConfirm);
		 	  
		  $(".confirm-main").show(); */
}

function showTestLabel(index) {
  showTest(index);

  $(".testPanel").show();
}

function clearTestState() {
  testStateArray[testExitIndex] = "";
}

function cancelPop() {
  $(".confirm-main").hide();
}

function confirmPop() {
  $(".confirm-main").hide();

  if (textResultIndex != 0) {
    $(".test-main").hide();

    testHided = true;

    setVideoPlay();
  }
}

function showTestPanel(index) {
  setVideoPause();

  testHided = false;

  $(".testPanel").show();

  showTest(index);
}

function exitTestPanel() {
  if (answerTypeArray[testIndex][1] == "answered") {
    $(".testPanel").hide();

    setVideoPlay();
  } else {
    alert("请作答提交后退出!");
  }
}
